package jlib.component.scroll
{
	import jlib.core.geom.Vector2D;
	import jlib.utils.ArrangeUtil;
	import jlib.utils.DepthUtil;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	// TODO : 세로형 스킨도 다 커버 가능하게 
	public class ScrollBarHorizontalState extends ScrollTrackManager implements IScrollBarDirectionState
	{
		private var size:Number;
		
		public function ScrollBarHorizontalState($scrollbar:ScrollBar): void
		{
			super($scrollbar);
		}
		
		public function changeThumbScale(): void
		{
			var totalOffset:Number = m_scrollbar.getThumbStartOffset()+m_scrollbar.getThumbEndOffset();
			var range:Number = m_scrollbar.getRange();
			var scalable:Boolean = m_scrollbar.getThumbScalable();
			
			if(!m_thumb || !m_track || !m_target) return;
			if(m_target.width/range == 0) return;
			if(scalable) m_thumb.width = m_track.width*(1/(m_target.width/range))-totalOffset;
			else m_thumb.scaleX = 1;
		}
		
		public function changeTargetPosition(): void
		{
			var range:Number = m_scrollbar.getRange();
			var guide:Number = m_scrollbar.getGuide();
			if(!m_target) return;
			
			var bound:Rectangle = m_target.getBounds(m_target);
			m_target.x = guide - (bound.x*m_target.scaleX + (bound.width*m_target.scaleX-range)*getPosition());
		}
		
		public function hasScrollBar(): Boolean 
		{ 
			var range:Number = m_scrollbar.getRange();
			if(!m_target) return false;
			return ((m_target.width/range) > 1); 
		}
		
		public function getTrackSize(): Number { return size; }
		public function setTrackSize(sizePixel:Number): void 
		{ 
			size = sizePixel;
			m_scrollbar.getTrack().width = size;
		}
		
		override protected function onThumbMove(e:Event):void
		{
			if(m_isDisposed) dispose();
			setPosition(m_thumbDownDistance+(m_stage.mouseX-m_downPosition.x)/m_movableLine.getDistance());
		}
		
		override protected function onTrackClick(e:Event):void 
		{
			if(m_isDisposed) dispose();
			if(e.target != m_track) return ;
			var totalOffset:Number = m_scrollbar.getThumbStartOffset()+m_scrollbar.getThumbEndOffset();
			setPosition(((m_track.mouseX-totalOffset)*m_track.scaleX-m_thumb.width*0.5)/m_movableLine.getDistance());
		}
		
		override protected function updateMovableLine():void 
		{
			var hdiff0:Number = (m_track.getBounds(m_track).width - m_thumb.getBounds(m_thumb).width)*0.5;
			var hdiff1:Number = (m_track.getRect(m_track).width - m_thumb.getRect(m_thumb).width)*0.5;
			var thickness:Number = hdiff1-hdiff0;
			var hdiff:Number = (m_track.getBounds(m_track).height - m_thumb.getBounds(m_thumb).height)*0.5;
			var start:Point = m_movableLine.getStart();
			var end:Point = m_movableLine.getEnd();
			start.x = m_track.x+m_scrollbar.getThumbStartOffset();
			end.x = m_track.x+(m_track.width-m_thumb.width)+m_scrollbar.getThumbEndOffset(); 
			start.y = end.y = m_track.y+thickness;
		}
		
		override protected function updateCurrentPosition(): void 
		{
			var totalOffset:Number = m_scrollbar.getThumbStartOffset()+m_scrollbar.getThumbEndOffset();
			m_currentPosition = ((m_thumb.x-m_track.x)-totalOffset)/m_movableLine.getDistance();
		}
		
	}//c
}